//###############
// FILE : SubdirDepthFilter.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION: Subdir filter object
//###############
package oop.ex3.fileSearchModel.filters;

import java.io.File;

import oop.ex3.fileSearchModel.filters.exceptions.FilterDataException;

/**
 * Filters files which are in a directory whose
 * depth below the source directory is as given
 */
class SubdirDepthFilter extends Filter {
	//***********************************************************************
	//  Data members :                                                      *
	//***********************************************************************
	String _sourceDir;
	int _intData;
	//***********************************************************************
	//  Methods :                                                           *
	//***********************************************************************
    /**
     * Constructs a new filter.
     */
    public SubdirDepthFilter(){
        super();
    }
    /**
     * Sets the condition of the filter.
     * @throws FilterDataException In case of wrong filter parameters. 
     */
    public void calculate() throws FilterDataException{
        Integer depth =0; 
        depth = _intData;
        if (depth<0){
            throw new FilterDataException ("Error:Invalid filter parameters");
        }
        if (checkCondition()){
            _condition = STATUS.TRUE;
        }
        else{
            _condition = STATUS.FALSE;
        }
    }
    /**
     * Checks the Filter's condition.
     * @return Filter's condition.
     * @throws FilterDataException In case of error in data value.
     */
    private boolean checkCondition() throws FilterDataException{
        String cutted = _file.getAbsolutePath().substring(
                _sourceDir.length(), _file.getAbsolutePath().length());
      
        if ( _intData == 0 && !cutted.contains(File.separatorChar+"") ) {
            return true;
        }else if ( cutted.contains(File.separatorChar+"") && 
                _intData != 0){
            String[] splitted = cutted.split(File.separatorChar+"");
            return ( splitted.length-1 == _intData );
        }
        return false;
        
    }
    /**
     * Resets filter's data information.
     * @param resetData Nodes data for reset.
     * @param source Source directory for check.
     */
    public void reset(File resetData, String source){
		super.reset(resetData, source);
		_sourceDir = source;
	}
    /**
	 *  Clones the object - return a new object with same properties.
	 */
    public Filter clone() {
        return new SubdirDepthFilter();
    }
	/**
	 * Sets the data of the filter.
	 * @param data The data to be set.
	 * @throws FilterDataException In case of wrong filter parameters. 
	 */
    public void setData(String data) throws FilterDataException{
    	try{
    		_intData = Integer.parseInt(data);
    	}
    	catch (NumberFormatException e){
    		throw new FilterDataException("Error:Invalid filter parameters");
    	}
    }
}
